package com.example.falvback.controller;

import cn.hutool.core.util.StrUtil;
import com.example.falvback.common.AuthAccess;
import com.example.falvback.common.Result;
import com.example.falvback.entity.User;
import com.example.falvback.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能：展示用户信息的接口，Controller层
 * 作者：chen
 * 日期：2023/10/2220:19
 */
@RestController
@RequestMapping("/user")
//@CrossOrigin//解决跨域问题
public class UserController {
    @Autowired
    UserService userService;//自动注入service层

    @GetMapping ("/show")
    public Result show() {
        List<User> userList = userService.showUser();
        return Result.success(userList);
    }
    @GetMapping ("/showPersonalFile/{username}")
    public Result showPersonalFile(@PathVariable String username) {
        List<User> user = userService.showUserFile(username);
        return Result.success(user);
    }
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success();
    }
    /**
     *  Controller重置密码
     */
    @AuthAccess
    @PutMapping("/password")
    public Result password(@RequestBody User user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPhone())) {
            return Result.error("数据输入不合法");
        }
        userService.resetPassword(user);
        return Result.success();
    }


}
